package com.anlogic.sdk.linker.ui;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.Item;

import com.anlogic.sdk.linker.Memory;

public class MemoryCellModifier implements ICellModifier {
	private LinkerViewer linkerViewer;

	public MemoryCellModifier(LinkerViewer linkerViewer) {
		this.linkerViewer = linkerViewer;
	}

	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {
		Memory m = (Memory) element;
		if (Memory.PROPERTY_NAME.equals(property))
			return m.getRealName();
		if (Memory.PROPERTY_BASE.equals(property))
			return m.getBase();
		if (Memory.PROPERTY_SIZE.equals(property))
			return m.getSize();
		return null;
	}

	public void modify(Object element, String property, Object value) {
		if (element instanceof Item)
			element = ((Item) element).getData();
		Memory m = (Memory) element;
		if (Memory.PROPERTY_NAME.equals(property)) {
			if (m.getName().equals(value))
				return;
			if (m.getAliasName().equals(value))
				return;
			this.linkerViewer.memoryRenamed(m.getRealName(), (String) value);
			m.setRealName((String) value);
		}
		if (Memory.PROPERTY_BASE.equals(property)) {
			if (m.getBase().equals(value))
				return;
			m.setBase((String) value);
		}
		if (Memory.PROPERTY_SIZE.equals(property)) {
			if (m.getSize().equals(value))
				return;
			m.setSize((String) value);
		}
		this.linkerViewer.modelChanged();
	}
}
